<template>
  <div class="login">
    <!-- 完成结构 -->
    <el-form ref="form" :model="form" :rules="rules" class="myform">
      <h3>iHRM 后台登录系统</h3>
      <el-form-item prop="mobile">
        <el-input
          v-model="form.mobile"
          prefix-icon="el-icon-user-solid"
          placeholder="请输入手机"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          show-password
          prefix-icon="el-icon-lock"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="btn" type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { loginApi } from '@/api/user.js'
export default {
  data () {
    return {
      form: {
        mobile: '13800000002', // 手机号
        password: '123456' // 密码
      },
      // 定义效验规则
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              // 定义手机号正则
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]1166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              // 测试数据
              const bool = reg.test(value)
              bool ? callback() : callback(new Error('手机号不正确'))
            },
            trigger: 'blur'
          }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    login () {
      // 完成全局的表单验证
      this.$refs.form.validate(async result => {
        if (result) {
          // 发送请求
          const res = await loginApi(this.form)
          // 接受响应
          console.log(res)
        } else {
          console.log('验证失败')
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login {
  background-image: url('../../assets/common/login.jpg');
  background-position: center;
  height: 100%;
  .myform {
    width: 520px;
    height: 400px;
    padding: 36px;
    padding-top: 0px;
    box-sizing: border-box;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -70%);
    h3 {
      font-size: 48px;
      font-weight: 200;
      letter-spacing: 1px; // 设置文本之间的间隔
      color: #fff;
      line-height: 1;
    }
    // 设置输入框的高度
    input.el-input__inner {
      height: 50px;
      background-color: rgba(255, 255, 255, 0.7);
      border: none;
      color: #68b0fe;
      border-radius: 6px;
      padding-left: 40px;
    }
    i.el-input__icon {
      color: #666;
    }
    .btn {
      width: 100%;
      height: 60px;
      font-size: 24px;
      background-color: #407ffe;
      border-radius: 6px;
    }
    .el-form-item__error {
      color: #fff;
    }
  }
}
</style>
